//
//  DetailViewController.m
//  关注和粉丝
//
//  Created by dllo on 16/9/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ConcernAndFanViewController.h"
#import "ZWYNetTool.h"
#import "MBProgressHUD.h"
#import "ConcernAndFan.h"
#import "ConcernAndFanTableCell.h"
#import "PersonalHomeViewController.h"
#import "UserInfo.h"

#define kConcernAndFanTableCell @"ConcernAndFanTableCellIdentifier"


@interface ConcernAndFanViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *fanView;
@property (weak, nonatomic) IBOutlet UIView *concernView;
@property (weak, nonatomic) IBOutlet UIButton *concernButton;
@property (weak, nonatomic) IBOutlet UIButton *fanButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSString *urlString;
@property (nonatomic, strong) ConcernAndFan *concernAndFan;
@property (nonatomic, strong) NSMutableArray<UserInfo *> *itemsArray;
@end

@implementation ConcernAndFanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self loadNewData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ConcernAndFanTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kConcernAndFanTableCell];
    _tableView.rowHeight = 80;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadNewData {
    NSString *urlParameter = nil;
    if (_clickType == MineHeaderClickLike) {
        [self setConcernColor];
        urlParameter = @"like";
    } else if (_clickType == MineHeaderClickLiked) {
        [self setFanColor];
        urlParameter = @"liked";
    } else {
        NSLog(@"除了关注和粉丝之外的东西");
    }
    [MBProgressHUD hideHUDForView:self.tableView animated:NO];
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.urlString = [NSString stringWithFormat:@"http://api.miaotu.net/v2/user/%@?token=abf776af-6a90-11e6-8ccf-00163e002e59&uid=abf7763c-6a90-11e6-8ccf-00163e002e59&page=1&num=10", urlParameter];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};    
    [ZWYNetTool GET:_urlString andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.concernAndFan = [ConcernAndFan modelWithDic:result];
        self.itemsArray = [NSMutableArray arrayWithArray:_concernAndFan.Items];
        [_tableView reloadData];
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _itemsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ConcernAndFanTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kConcernAndFanTableCell forIndexPath:indexPath];
    cell.items = _itemsArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_itemsArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
#warning 提交删除的网络请求
        
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalHomeViewController *personHomeVC = [[PersonalHomeViewController alloc] init];
    personHomeVC.Uid = _itemsArray[indexPath.row].Uid;
    [self.navigationController pushViewController:personHomeVC animated:YES];
}

- (IBAction)buttonClick:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"关注"]) {
        _clickType = MineHeaderClickLike;
    } else {
        _clickType = MineHeaderClickLiked;
    }
    [self loadNewData];
}

- (void)setConcernColor {
    _concernButton.backgroundColor = [UIColor whiteColor];
    [_concernButton setTitleColor:[UIColor colorWithRed:0.863 green:0.596 blue:0.161 alpha:1.000] forState:UIControlStateNormal];
    _concernView.backgroundColor = [UIColor colorWithRed:0.863 green:0.596 blue:0.161 alpha:1.000];
    _fanButton.backgroundColor = [UIColor colorWithRed:0.937 green:0.941 blue:0.961 alpha:1.000];
    [_fanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _fanView.backgroundColor = [UIColor colorWithRed:0.937 green:0.941 blue:0.961 alpha:1.000];
}

- (void)setFanColor {
    _fanButton.backgroundColor = [UIColor whiteColor];
    [_fanButton setTitleColor:[UIColor colorWithRed:0.863 green:0.596 blue:0.161 alpha:1.000] forState:UIControlStateNormal];
    [_fanButton setTitleColor:[UIColor colorWithRed:0.863 green:0.596 blue:0.161 alpha:1.000] forState:UIControlStateNormal];
    _fanView.backgroundColor = [UIColor colorWithRed:0.863 green:0.596 blue:0.161 alpha:1.000];
    _concernButton.backgroundColor = [UIColor colorWithRed:0.937 green:0.941 blue:0.961 alpha:1.000];
    [_concernButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _concernView.backgroundColor = [UIColor colorWithRed:0.937 green:0.941 blue:0.961 alpha:1.000];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
